cmake_minimum_required (VERSION 2.6)

project (BubblesOS)

find_package(Java COMPONENTS Development REQUIRED)

OPTION(BubblesOS_DEBUG  "Build the project using debugging code." ON)
OPTION(BubblesOS_ENDIAN "Build the kernel with little endian format." ON)
OPTION(BubblesOS_64bit  "Build the kernel as 64 bit version." ON)
OPTION(BubblesOS_X86    "Build the kernel for X86 CPU's." ON)

INCLUDE(cmake/findsystem.txt)
INCLUDE(cmake/findnasm.txt)
INCLUDE(cmake/findgplusplus.txt)
INCLUDE(cmake/findar.txt)
INCLUDE(cmake/findranlib.txt)
INCLUDE(cmake/findgcc.txt)
INCLUDE(cmake/findld.txt)
INCLUDE(cmake/setkernelflags.txt)
INCLUDE(cmake/datetime.txt)

SET(BUBBLESOS_VERSION "0.0.1")

NOW(BUBBLESOS_BUILD_TIME)
SET(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
SET(BUBBLESOS_BUILD_SYSTEM "${SYSTEM} - ${CMAKE_SYSTEM_PROCESSOR}")
SET(BUBBLESOS_BUILD_FLAGS  "${KFLAGS} ${WFLAGS} ${OPTIMIZEFLAGS}")

IF(BubblesOS_ENDIAN)
  SET(ENDIAN PL_LITTLE_ENDIAN)
ELSE(BubblesOS_ENDIAN)
  SET(ENDIAN PL_BIG_ENDIAN)
ENDIF(BubblesOS_ENDIAN)
IF(BubblesOS_64bit)
  SET(PL_64BIT ON)
ELSE(BubblesOS_64bit)
  SET(PL_32BIT ON)
ENDIF(BubblesOS_64bit)
IF(BubblesOS_X86)
  SET(PL_X86 ON)
ELSE(BubblesOS_X86)
  MESSAGE(FAILED " No architecture set.")
ENDIF(BubblesOS_X86)

MESSAGE("-- Bubbles OS Version ${BUBBLESOS_VERSION}")
MESSAGE("-- Build time: ${BUBBLESOS_BUILD_TIME}")

IF(BubblesOS_DEBUG)
  MESSAGE("-- Debug Messages on")
ELSE(BubblesOS_DEBUG)
  MESSAGE("-- Debug Messages off")
ENDIF(BubblesOS_DEBUG)
IF(BubblesOS_ENDIAN)
  MESSAGE("-- Endian format little")
ELSE(BubblesOS_ENDIAN)
  MESSAGE("-- Endian format big")
ENDIF(BubblesOS_ENDIAN)
IF(BubblesOS_64bit)
  MESSAGE("-- Build as 64bit")
ELSE(BubblesOS_64bit)
  MESSAGE("-- Build as 32bit")
ENDIF(BubblesOS_64bit)
IF(BubblesOS_X86)
  SET(BUBBLESOS_ARCHITECTURE "x86")
  MESSAGE("-- Build for X86")
ELSE(BubblesOS_X86)
  MESSAGE(FAILED " No architecture set.")
ENDIF(BubblesOS_X86)

IF(BubblesOS_DEBUG)
  SET(CMAKE_BUILD_TYPE DEBUG)
ENDIF(BubblesOS_DEBUG)

FIND_PACKAGE(Mercurial)
IF(MERCURIAL_FOUND)
  MERCURIAL_HG_INFO(${PROJECT_SOURCE_DIR} Project)
ENDIF(MERCURIAL_FOUND)
CONFIGURE_FILE(${PROJECT_SOURCE_DIR}/data/startup.ini.in ${PROJECT_SOURCE_DIR}/data/startup.ini)

EXECUTE_PROCESS(COMMAND ${JAVA_COMPILE} -version
    OUTPUT_VARIABLE JAVA_OUTPUT
    ERROR_VARIABLE  JAVA_ERROR_OUTPUT
    RESULT_VARIABLE JAVA_RETURN_VALUE
)
IF(NOT JAVA_RETURN_VALUE)
    STRING(REGEX MATCH [0-9._]+ JAVA_VERSION ${JAVA_ERROR_OUTPUT})
ENDIF(NOT JAVA_RETURN_VALUE)

MESSAGE("-- system is ${SYSTEM}")
MESSAGE("-- system revision    is: ${Project_HG_ID}")
MESSAGE("-- Kernel nasm   command: ${nasm}")
MESSAGE("-- Kernel g++    command: ${gplusplus}")
MESSAGE("-- Kernel gcc    command: ${gcc}")
MESSAGE("-- Kernel ld     command: ${ld}")
MESSAGE("-- Kernel ar     command: ${gar}")
MESSAGE("-- Kernel runlib command: ${granlib}")
MESSAGE("-- Kernel C        flags: ${KCFLAGS}")
MESSAGE("-- Kernel C++      flags: ${KFLAGS}")
MESSAGE("-- Kernel warning  flags: ${WFLAGS}")
MESSAGE("-- Kernel optimize flags: ${OPTIMIZEFLAGS}")
MESSAGE("-- Kernel include  dirs : ${KERNELINCLUDE}")
MESSAGE("-- Kernel JAVA version  : ${JAVA_VERSION}")
MESSAGE("-- Kernel JAVA runtime  : ${JAVA_RUNTIME}")
MESSAGE("-- Kernel JAVA compile  : ${JAVA_COMPILE}")

ADD_SUBDIRECTORY(kernel)
#ADD_SUBDIRECTORY(java)
#ADD_SUBDIRECTORY(lib)
#ADD_SUBDIRECTORY(driver)

SET(DRIVER_JAVA_SRCS
    com/BubblesOS/System/Driver/Pit/Pit.java
)

SET(JAVA_LANG_SRCS
    java/lang/Object.java
)

ADD_CUSTOM_TARGET(java.driver.class
#-bootclasspath ${PROJECT_SOURCE_DIR}:${PROJECT_SOURCE_DIR}/java/sdk
    COMMAND ${JAVA_COMPILE} -d ${CMAKE_CURRENT_BINARY_DIR} ${DRIVER_JAVA_SRCS}
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)

ADD_CUSTOM_TARGET(java.lang.class
#-bootclasspath ${PROJECT_SOURCE_DIR}/java/sdk
    COMMAND ${JAVA_COMPILE} -d ${CMAKE_CURRENT_BINARY_DIR} ${JAVA_LANG_SRCS}
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/java/sdk
)

INCLUDE(cmake/createimage.txt)

INCLUDE(FindDoxygen)
IF (DOXYGEN)
  ADD_SUBDIRECTORY(doc/kernel)
  ADD_SUBDIRECTORY(doc/libc)
  ADD_SUBDIRECTORY(doc/libcxx)
  ADD_SUBDIRECTORY(doc/libosdk)
ELSE (DOXYGEN)
  MESSAGE(STATUS "WARNING: Doxygen not found - Reference manual will not be created")
ENDIF (DOXYGEN)
